package com.weddingee.dao.home;
import com.weddingee.dao.base.BaseDao;
import com.weddingee.exception.DBException;
import com.weddingee.exception.DaoException;
import com.weddingee.model.Other;
import com.weddingee.model.User;
import com.weddingee.model.News;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;

public class HomeDao extends BaseDao{
	
	
	//save
	public User save(User user){
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "insert into user values(null,?,?,?,?,null,null,null,null,null,null,null)";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				//USERNAME
				pstm.setString(1,  user.getUsername());
				//PASSWORD
				pstm.setString(2,  user.getPassword());
				//REALNAME
				pstm.setString(3, user.getRealname());
				//Type
				pstm.setString(4, user.getType());

				int res = pstm.executeUpdate();
				if( res > 0){
					ResultSet rset = pstm.getGeneratedKeys();
					rset.next();
					user.setUserId(rset.getLong(1));
					return user;
				}
				
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
	}
	
	//login
	 public String login(User user){
		  return "SUCCESS";
	  }
	 //checkUser Exist or not
	  public boolean checkUser(String username){
		  Connection conn = null;
			PreparedStatement pstm  = null;
				try {
					conn = getConn();
					String sql = "select * from user where USERNAME= ?";
					pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					
					//newsId
					pstm.setString(1, username);
					
					
					ResultSet rset = pstm.executeQuery();
					if(false==rset.next())
					{return false;}
					String sUsername = rset.getString(2);
					if(sUsername==null){
					return true;
					}
					
					return false;
					
				} catch (Exception e) {
					e.printStackTrace();
					throw new DaoException("error",e);
				} finally{
					try {
						closeStatement(pstm);
						closeConn(conn);
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
	  }
	//list
	 public List<User> listUser(int page,String userType){
		 Connection conn = null;
			PreparedStatement pstm  = null;
			
			 List<User> userList = new ArrayList<User>();
				try {
					conn = getConn();
					String sql = "SELECT * FROM user where TYPE = ? order by id asc LIMIT ?,24";
					pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					pstm.setString(1, userType);
					pstm.setInt(2,  (page-1)*10);
					ResultSet rset = pstm.executeQuery();
					int i=0;
					while(rset.next())
					{
					User user = new User();
					user.setUserId(rset.getLong(1));
					user.setUsername(rset.getString(2));
					user.setPassword(rset.getString(3));
					user.setRealname(rset.getString(4));
					user.setType(rset.getString(5));
					user.setProfile1(rset.getString(6));
					user.setProfile2(rset.getString(7));
					user.setProfile3(rset.getString(8));
					user.setProfile4(rset.getString(9));
					user.setProfile5(rset.getString(10));
					user.setProfile6(rset.getString(11));
					user.setHeadPic(rset.getString(12));
					userList.add(i,user);
					i++;
					}
				    return userList;
					
					
				} catch (Exception e) {
					e.printStackTrace();
					throw new DaoException("error",e);
				} finally{
					try {
						closeStatement(pstm);
						closeConn(conn);
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
			
			
	  }
	 
	 //delete
	 public String delete(Long userId){
		 Connection conn = null;
			PreparedStatement pstm  = null;
				try {
					conn = getConn();
					String sql = "delete from user where id= ?";
					pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					
					//newsId
					pstm.setLong(1, userId);
					int res = pstm.executeUpdate();
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					throw new DaoException("error");
				} finally{
					try {
						closeStatement(pstm);
						closeConn(conn);
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
	 }
	 
	 //find
	 public User find(Long userId){
		 Connection conn = null;
			PreparedStatement pstm  = null;
			User user = new User();
				try {
					conn = getConn();
					String sql = "select * from user where id= ?";
					pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					
					//newsId
					pstm.setLong(1, userId);
					
					
					ResultSet rset = pstm.executeQuery();
					rset.next();
					user.setUserId(userId);
					user.setUsername(rset.getString(2));
					user.setPassword(rset.getString(3));
					user.setRealname(rset.getString(4));
					user.setType(rset.getString(5));
					user.setProfile1(rset.getString(6));
					user.setProfile2(rset.getString(7));
					user.setProfile3(rset.getString(8));
					user.setProfile4(rset.getString(9));
					user.setProfile5(rset.getString(10));
					user.setProfile6(rset.getString(11));
					user.setHeadPic(rset.getString(12));
					user.setIntroduce(rset.getString(13));
					return user;
					
					
				} catch (Exception e) {
					e.printStackTrace();
					throw new DaoException("error",e);
				} finally{
					try {
						closeStatement(pstm);
						closeConn(conn);
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
	 }
	 
	 //update
	 public User update(User user){
			Connection conn = null;
			PreparedStatement pstm  = null;
				try {
					conn = getConn();
					String sql = "update user set USERNAME=? , HEAD_PIC=? , TYPE=? , PROFILE_1=?,PROFILE_2=?,PROFILE_3=?,PROFILE_4=?,PROFILE_5=?,PROFILE_6=? ,PASSWORD=?,REALNAME=?,INTRODUCE=?,PROFILE_7=? where id= ?";          
					pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					//NAME
					pstm.setString(1,  user.getUsername());
					//HEAD_PIC
					pstm.setString(2,  user.getHeadPic());
					//Type
					pstm.setString(3, user.getType());
					//PROFILE_1
					pstm.setString(4, user.getProfile1());
					//PROFILE_2
					pstm.setString(5, user.getProfile2());
					//PROFILE_3
					pstm.setString(6, user.getProfile3());
					//PROFILE_4
					pstm.setString(7, user.getProfile4());
					//PROFILE_5
					pstm.setString(8, user.getProfile5());
					//PROFILE_6
					pstm.setString(9, user.getProfile6());	
					//PASSWORD
					pstm.setString(10, user.getPassword());
					//REALNAME
					pstm.setString(11,user.getRealname());
					//INTRODUCR
					pstm.setString(12,user.getIntroduce());
					//PROFILE_7
					pstm.setString(13, user.getProfile7());	
					//id
					pstm.setLong(14, user.getUserId());
					int res = pstm.executeUpdate();
					return user;

				} catch (Exception e) {
					e.printStackTrace();
					throw new DaoException("error");
				} finally{
					try {
						closeStatement(pstm);
						closeConn(conn);
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
	 }
	 
}